package com.pansky.crm.service.impl;

import com.pansky.crm.common.mybatisplus.ApiResult;
import com.pansky.crm.config.redis.RedisBean;
import com.pansky.crm.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * @author ：zhouzhiyong
 * @date ：Created in 2021/3/15 11:46
 * @description：文件服务
 */
@Service
public class FileService {

    @Autowired
    private RedisBean redisBean;

    public ApiResult<Boolean> removeFile(String filePath, String token) throws FileNotFoundException {
        String path = ResourceUtils.getURL("classpath:").getPath()+"static" + File.separator + "upload" + File.separator + "image" + File.separator;
        File file = new File(path + filePath.substring(filePath.lastIndexOf("/")));
        if (file.exists()) {
            if (file.delete()) {
                String subject = TokenUtil.getTokenBody(token).getSubject();
                redisBean.del("exist_File:" + subject);
                return ApiResult.ok();
            }
        }
        return ApiResult.failed("删除文件失败");
    }
}
